package com.issoft.studentresitor.controller;

import com.issoft.studentresitor.domain.pojo.Result;
import com.issoft.studentresitor.service.IStudentService;
import com.issoft.studentresitor.utils.AliyunOSSOperator;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

@Slf4j
@RestController
@Api(tags = "图片上传接口")
public class UploadController {
    @Autowired
    private AliyunOSSOperator aliyunOSSOperator;
    @Autowired
    private IStudentService studentService;
    @ApiOperation("上传图片")
    @PostMapping("/upload")
    public Result upload(MultipartFile file) throws Exception {
        log.info("文件参数{},", file.getOriginalFilename());
        //保存文件
        String url =aliyunOSSOperator.upload(file.getBytes(), file.getOriginalFilename());
        log.info("文件上传oss,url地址{}", url);
        return Result.success(url);
    }
    @ApiOperation("上传图片带上user_id")
    @PostMapping("/uploadWithUserId")
    public Result uploadWithUserId(MultipartFile file,Integer userId) throws Exception {
        log.info("文件参数{},", file.getOriginalFilename());
        //保存文件
        String url =aliyunOSSOperator.upload(file.getBytes(), file.getOriginalFilename());
        log.info("文件上传oss,url地址{}", url);
        studentService.updateStuPhoto(userId,url);
        return Result.success(url);
    }

}
